var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);


// 获取热门搜索
router.get('/jkyx/pc/get_hot_search', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getAllHotSearch,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			hots: results,
	     		}));	         
			}
		})
	})
});


// 新增热门搜索
router.get('/jkyx/pc/new_hot_search', function(req, res, next) {
	var param = req.query || req.params; 
	var keyword = param.keyword || '';
	var sort = param.sort || '0';
	var status = param.status || '1';
	var timer = CommonJS.getNowFormatDate();
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_new_hotSearch,[keyword,sort,status,timer],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var hots = results[1];
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			hots: hots
	     		}));	         
			}
		})
	})
});
// 编辑热门搜索
router.get('/jkyx/pc/edit_hot_search', function(req, res, next) {
	var param = req.query || req.params; 
	var id = param.id || '';
	var keyword = param.keyword || '';
	var sort = param.sort || '0';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_edit_HotSearchByID,[keyword,sort,id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var hots = results[1];
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			hots: hots
	     		}));	         
			}
		})
	})
});

// 编辑热门搜索
router.get('/jkyx/pc/delete_hot_search', function(req, res, next) {
	var param = req.query || req.params; 
	var id = param.id || '';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_delete_HotSearchByID,[id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var hots = results[1];
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			hots: hots
	     		}));	         
			}
		})
	})
});

module.exports = router;